<template>
  <div class="mod-config">
    <el-form size="small" :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="searchForm.realname" placeholder="请输入姓名" clearable style="width: 160px;"></el-input>
        <el-input
          v-model="searchForm.identityCard"
          placeholder="请输入身份证号"
          clearable
          style="width: 160px;"
        ></el-input>
        <el-input v-model="searchForm.mobile" placeholder="请输入手机号码" clearable style="width: 160px;"></el-input>
      </el-form-item>
      <el-form-item prop="isDel">
        <el-radio-group size="small" v-model="searchForm.isDel">
          <el-radio-button :label="0">未删除</el-radio-button>
          <el-radio-button :label="1">已删除</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList(1)">查询</el-button>
        <!--        <el-button-->
        <!--          type="danger"-->
        <!--          @click="deleteHandle()"-->
        <!--          :disabled="dataListSelections.length <= 0"-->
        <!--        >批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;"
    >
      <!--      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
      <el-table-column align="center" type="index" header-align="center" width="80" label="ID"></el-table-column>
      <el-table-column prop="name" header-align="center" align="center" label="所属租户"></el-table-column>
      <el-table-column prop="realname" header-align="center" align="center" label="真实姓名"></el-table-column>
      <el-table-column prop="gender" header-align="center" align="center" width="80" label="性别">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.gender==1">男</el-tag>
          <el-tag v-else-if="scope.row.gender==2">女</el-tag>
          <el-tag v-else>未知</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="identity_card" header-align="center" align="center" label="身份证号"/>
      <el-table-column prop="mobile" header-align="center" align="center" label="手机号"></el-table-column>
      <el-table-column prop="duty" header-align="center" align="center" label="职务"></el-table-column>
      <!--      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.userId)">修改密码</el-button>-->
      <!--          <el-button-->
      <!--            type="text"-->
      <!--            size="small"-->
      <!--            @click="deleteHandle(scope.row.userId,scope.row.realname)"-->
      <!--          >删除</el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 /查看/工作经历 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './member-add-or-update'

  export default {
    data() {
      return {
        dataForm: {
          key: '',
          gName: '',
          identityCard: '',
          mobile: ''
        },
        searchForm: {
          realname: '',
          identityCard: '',
          mobile: '',
          isDel: ''
        },
        dataList: [],
        quesCates: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        experienceVisible: false,
        batchImportVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated() {
      this.getDataList()
    },
    methods: {
      batchImportHandleH() {
        this.batchImportVisible = true
        this.$nextTick(() => {
          this.$refs.batchImport.init(4)
        })
      },
      // 获取数据列表
      getDataList(page) {
        if (page) this.pageIndex = page
        this.dataListLoading = true
        this.$http({
          url: '/user/super/list',
          method: 'post',
          data: {
            page: this.pageIndex,
            limit: this.pageSize,
            realname: this.searchForm.realname,
            identityCard: this.searchForm.identityCard,
            isDel: this.searchForm.isDel,
            mobile: this.searchForm.mobile
          }
        }).then(data => {
          console.log(data)
          if (data && data.code == 200) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      //违章记录
      experienceHandle(id) {
        this.experienceVisible = true
        this.$nextTick(() => {
          this.$refs.experience.init(id)
        })
      },
      // 工种类型
      seeDetails(id) {
        this.seeDetailsVisible = true
        this.$nextTick(() => {
          this.$refs.seeDetails.init(id)
        })
      },
      // 删除
      deleteHandle(id, realname) {
        var ids = id
          ? [id]
          : this.dataListSelections.map(item => {
            return item.userId
          })
        var name = realname
          ? [realname]
          : this.dataListSelections.map(item => {
            return item.userId
          })
        this.$confirm(
          `确定对[${name.join(',')}]进行[${realname ? '删除' : '批量删除'}]操作?`,
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          this.$http({
            url: '/user/member/delete',
            method: 'post',
            data: {ids: ids}
          }).then(data => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      uploadFile(file) {
        let data = new FormData()
        data.append('file', file)
        this.$http({
          url: '/import/user',
          method: 'post',
          data: data
        }).then(res => {
          if (res.code == 200) {
            this.$message.success('导入成功')
            this.getDataList()
            this.batchImportVisible = false
          } else {
            this.$message.error(res.msg)
            this.$refs.batchImport.resetFile()
          }
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v =>
          filterVal.map(j => {
            return v[j]
          })
        )
      }
    }
  }
</script>
